﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.ComponentModel;
using Fasterflect;
using LL.Data.Mapping;

namespace LL.Data.FluentData.Mapping
{
    public class DefaultMapper : IMapper
    {
        private Type _type;

        public DefaultMapper(Type type)
        {
            this._type = type;
        }

        public string GetTableName()
        {
            var attr = _type.Attributes<TableAttribute>().FirstOrDefault();
            if (attr == null)
                return _type.Name;
            return attr.TableName;
        }


        public string[] GetPrimaryKeyNames()
        {
            var list = (from d in _type.GetProperties()
                        where d.Attribute<PrimaryKeyAttribute>() != null
                        select d.Name).ToArray();
            return list.Length > 0 ? list : new string[] { "Id" };
        }


        public string[] GetIgnoreColumnNames()
        {
            var list = from d in _type.GetProperties()
                       where d.Attribute<IgnoreAttribute>() != null
                       select d.Name;
            return list.ToArray();
        }


        public string[] GetColumnsNames()
        {
            var list = from d in _type.GetProperties()
                       where d.Attribute<IgnoreAttribute>() == null
                       select d.Name;
            return list.ToArray();
        }


        public string[] GetAutoIncrementColumnNames()
        {
            var list = from d in _type.GetProperties()
                       where d.Attribute<AutoIncrementAttribute>() != null
                       select d.Name;
            return list.ToArray();
        }
    }
}
